/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014-2016 abel533@gmail.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.is.add.controller.restful;

import com.github.pagehelper.PageInfo;
import com.is.add.controller.vo.DataCountPageListVO;
import com.is.add.enmus.OrderStatus;
import com.is.add.enmus.OrderType;
import com.is.add.enmus.PayType;
import com.is.add.enmus.TimeType;
import com.is.add.model.OrderBase;
import com.is.add.service.OrderService;
import com.is.add.util.DimensionUtil.DataCount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liuzh
 * @since 2015-12-19 11:10
 */
@RestController
@RequestMapping("api/order")
@CrossOrigin
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 条件统计订单数据
     * @param dimensions    维度
     * @param timeType      时间类型
     * @param orderStatus   订单状态
     * @param startTime     开始时间
     * @param endTime       结束时间
     * @param payType       支付类型
     * @param orderType     订单类型
     * @param city          城市
     * @param sportsType    运动类型
     * @param searchKey     关键字
     * @param groups        省份
     * @return              统计Settlement
     */
    @RequestMapping(value = "/count/orderData")
    public DataCount countOrderData(
            @RequestParam String dimensions,
            @RequestParam TimeType timeType,
            @RequestParam OrderStatus orderStatus,
            @RequestParam String startTime,
            @RequestParam String endTime,
            @RequestParam(required = false) PayType payType,
            @RequestParam(required = false) OrderType orderType,
            @RequestParam(required = false) String city,
            @RequestParam(required = false) String sportsType,
            @RequestParam(required = false) String searchKey,
            @RequestParam(required = false) String groups) {
        Map<String,String> map =new HashMap<>();
        String[] dimensionList =dimensions.trim().split(",");
        if (payType != null){
            map.put("payType",payType.name());
        }
        if (orderType != null){
            map.put("orderType",orderType.name());
        }
        if (city != null){
            map.put("city",city);
        }
        if (sportsType != null){
            map.put("sportsType",sportsType);
        }
        if (groups != null){
            map.put("groups",groups);
        }
        if (searchKey != null){
            map.put("searchKey",searchKey);
        }
        startTime = startTime+" 00:00:00";
        endTime = endTime + " 23:59:59";
        map.put("orderStatus",orderStatus.name());
        map.put("timeType",timeType.name());
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        return this.orderService.getAllOrder(dimensionList,map);
    }

    /**
     * 统计明细
     * @param timeType      时间类型
     * @param orderStatus   订单状态
     * @param shop          场馆
     * @param sales         负责人
     * @param startTime     开始时间
     * @param endTime       结束时间
     * @param payType       支付类型
     * @param orderType     订单类型
     * @param city          城市
     * @param sportsType    运动类型
     * @param searchKey     关键字
     * @param groups        省份
     * @param pageNum       页码
     * @param pageSize      每页数量
     * @return              统计Settlement
     */
    @RequestMapping(value = "/load/dataCountDetails")
    public PageInfo<OrderBase> getDataCountDetails(
            @RequestParam TimeType timeType,
            @RequestParam String startTime,
            @RequestParam String endTime,
            @RequestParam OrderStatus orderStatus,
            @RequestParam(required = false) String shop,
            @RequestParam(required = false) String sales,
            @RequestParam(required = false) PayType payType,
            @RequestParam(required = false) OrderType orderType,
            @RequestParam(required = false) String city,
            @RequestParam(required = false) String sportsType,
            @RequestParam(required = false) String searchKey,
            @RequestParam(required = false) String groups,
            @RequestParam(required = false,defaultValue = "1") int pageNum,
            @RequestParam(required = false,defaultValue = "20") int pageSize
            ) {
        Map<String,String> map =new HashMap<>();
        if (shop != null && !"".equals(shop)){
            map.put("shop", shop);
        }
        if (sales != null && !"".equals(sales)){
            map.put("sales",sales);
        }
        if (payType != null){
            map.put("payType",payType.name());
        }
        if (orderType != null){
            map.put("orderType",orderType.name());
        }
        if (city != null){
            map.put("city",city);
        }
        if (sportsType != null){
            map.put("sportsType",sportsType);
        }
        if (groups != null){
            map.put("groups",groups);
        }
        if (searchKey != null && !"".equals(searchKey)){
            map.put("searchKey",searchKey);
        }
        startTime = startTime+" 00:00:00";
        endTime = endTime + " 23:59:59";
        map.put("orderStatus",orderStatus.name());
        map.put("timeType",timeType.name());
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        List<OrderBase> countDetails =  this.orderService.getDataCountDetails(map,pageNum,pageSize);
        return new PageInfo<>(countDetails);
    }

    /**
     * 数据统计页选择项
     * @return  键值对list
     */
    @RequestMapping(value = "/selectList")
    public DataCountPageListVO selectList(){
        return this.orderService.getSelectList();
    }
}
